<?php
/**
 * Created by PhpStorm.
 * User: shutao
 * Date: 2018/9/13
 * Time: 13:31
 */

namespace wechat\models;

use tt\common\components\TtActiveRecord;
use ttcommon\base\Arrays;
use ttcommon\base\Data;
use yii\base\UserException;
use yii\db\Migration;

/**
 * 微信公众号配置表
 *
 * @property integer $id
 * @property string $title
 * @property string $appid
 * @property string $appsecret
 * @property string $token
 * @property string $encodingaeskey
 * @property integer $debug
 * @property string $domain
 * @property integer $status
 * @property integer $type
 * @property string $qr_code
 * @property integer $created_at
 * @property integer $is_default
 */
class WeConfig extends TtActiveRecord
{
    public static function tableName()
    {
        return 'we_config';
    }

    public function createTableWithName(Migration $migration, $tableName)
    {
        $migration->createTable($tableName, [
            'id' => $migration->primaryKey()->unsigned(),
            'title' => $migration->string(50)->notNull()->unique()->comment($this->getAttributeLabel('title')),
            'appid' => $migration->string(20)->notNull()->unique()->comment($this->getAttributeLabel('appid')),
            'appsecret' => $migration->string(50)->notNull()->comment($this->getAttributeLabel('appsecret')),
            'token' => $migration->string(50)->notNull()->comment($this->getAttributeLabel('token')),
            'encodingaeskey' => $migration->string(50)->notNull()->comment($this->getAttributeLabel('encodingaeskey')),
            'debug' => $migration->tinyInteger(1)->notNull()->defaultValue(self::DEBUG_CLOSE)->unsigned()->comment($this->getAttributeLabel('debug')),
            'domain' => $migration->string(100)->notNull()->defaultValue('')->comment($this->getAttributeLabel('domain')),
            'status' => $migration->tinyInteger(1)->notNull()->defaultValue(self::STATUS_ACTIVE)->comment($this->getAttributeLabel('status')),
            'type' => $migration->tinyInteger(1)->notNull()->comment($this->getAttributeLabel('type')),
            'qr_code' => $migration->string(100)->notNull()->defaultValue('')->comment($this->getAttributeLabel('qr_code')),
            'created_at' => $migration->integer()->notNull()->defaultValue(0)->comment($this->getAttributeLabel('created_at')),
            'is_default' => $migration->tinyInteger(1)->notNull()->defaultValue(0)->comment($this->getAttributeLabel('is_default'))
        ]);
    }

    const DEBUG_CLOSE = 0;
    const DEBUG_OPEN = 1;

    public static function debugText($debug = null)
    {
        static $text = [
            self::DEBUG_CLOSE => '关闭',
            self::DEBUG_OPEN => '开启'
        ];
        return Arrays::constText($text, $debug);
    }

    const STATUS_DISABLED = 0;
    const STATUS_ACTIVE = 1;

    public static function statusText($status = null)
    {
        static $text = [
            self::STATUS_DISABLED => '禁用',
            self::STATUS_ACTIVE => '开启'
        ];
        return Arrays::constText($text, $status);
    }

    const TYPE_UNKNOWN = 0;
    const TYPE_GZH = 1;
    const TYPE_XCX = 2;

    public static function typeText($type = null)
    {
        static $text = [
            self::TYPE_UNKNOWN => '未知',
            self::TYPE_GZH => '公众号',
            self::TYPE_XCX => '小程序'
        ];
        return Arrays::constText($text, $type);
    }

    const IS_DEFAULT_YES = 1;
    const IS_DEFAULT_NO = 0;

    public static function isDefaultText($is_default = null)
    {
        static $text = [
            self::IS_DEFAULT_NO => '否',
            self::IS_DEFAULT_YES => '是'
        ];
        return Arrays::constText($text, $is_default);
    }

    public function attributeLabels()
    {
        return [
            'id' => '#',
            'title' => '名称',
            'appid' => 'appID',
            'appsecret' => 'appsecret',
            'token' => 'token',
            'encodingaeskey' => 'encodingaeskey',
            'debug' => '开启调试',
            'domain' => 'JS接口安全域名',
            'status' => '状态',
            'created_at' => '创建日期',
            'type' => '类型',
            'is_default' => '是否默认',
            'qr_code' => '二维码'
        ];
    }

    public function scenarios()
    {
        return [
            self::SCENARIO_DEFAULT => [
                'title', 'appid', 'appsecret', 'token', 'encodingaeskey', 'debug', 'domain', 'status', 'created_at', 'type', 'is_default', 'qr_code'
            ]
        ];
    }

    public function rules()
    {
        return [
            [
                ['debug', 'status', 'type', 'is_default'], 'integer'
            ],
            ['title', 'string', 'length' => [2, 50]],
            ['appid', 'string', 'length' => [18, 50]],
            ['appsecret', 'string', 'length' => [2, 50]],
            ['token', 'string', 'length' => [2, 50]],
        ];
    }

    public function beforeSave($insert)
    {
        if ($insert) {
            $this->created_at = time();
        }
        if (self::TYPE_GZH == $this->type) {
            if (!$this->token) {
                throw new UserException('公众号必须填写 toke');
            }
        }
        $default = (int)Data::isTrue($this->is_default);
        if ($default) { // 如果设置为默认，那么就必须为激活
            WeConfig::updateAll(['is_default' => self::IS_DEFAULT_NO], ['type' => $this->type]);
            $this->status = self::STATUS_ACTIVE;
        }
        $this->is_default = $default;
//        pr($this->toArray());
        return parent::beforeSave($insert);
    }

    public function changeAttrs($status)
    {
        switch ($status) {
            case 'status':
                $this->status = intval(!$this->status);
                break;
            case 'debug':
                $this->debug = intval(!$this->debug);
                break;
            case 'is_default':
                $this->is_default = intval(!$this->is_default);
                break;
            default:
                break;
        }
    }
}